package com.xingkeduo.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: 中文输入法发挥不稳定的刘川
 * @Date: 19-3-13 上午11:17
 */

public class TimeUtil {

    public static Date getDateOfMonday(String today,int afterNum) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(today);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return getDateOfMonday(date,afterNum);
    }

    /**
     * @description  获取传入时间的当前周一日期
     * @Param [date, afterNum]
     * @return java.util.Date
     **/
    public static Date getDateOfMonday(Date date,int afterNum) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        //设置一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        cal.add(Calendar.DATE, 7*afterNum);
        return cal.getTime();
    }

    public static int getWeekDayForDate(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int day=calendar.get(Calendar.DAY_OF_WEEK);
        day--;
        return day>0?day:7;
    }

    /**
     * @description  获取传入时间的当前周日日期
     * @Author 情绪极其不稳定的Shadow
     * @Param [date]
     * @return java.lang.String
     **/
    public static Date getDateOfSunday(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(cal.DATE) + 6);
        return cal.getTime();
    }


}
